package com.bluesmart.mia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DeviceInfoActivity extends BaseActivity {
	JSONObject mObj = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_info);
		
		mObj = JSON.parseObject(getIntent().getStringExtra("obj"));

		((TextView) findViewById(R.id.center)).setText(getString(R.string.devices));
		((Button) findViewById(R.id.right)).setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.a07, 0);
		
		findViewById(R.id.right).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				sendHttpRequest();
			}
		});
		findViewById(R.id.left).setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		sendHttpRequest();
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				
				JSONObject json = JSON.parseObject(response);
				
				((TextView)findViewById(R.id.text0)).setText(json.getString("deviceName"));
				((TextView)findViewById(R.id.text1)).setText(json.getString("macaddress"));
				((TextView)findViewById(R.id.text2)).setText(json.getString("time"));
				
				float bare = json.getFloatValue("deviceElectricity");
				if(bare < 50) ((ImageView)findViewById(R.id.bare)).setImageResource(R.drawable.a47);
				else if(bare >= 50 && bare < 75) ((ImageView)findViewById(R.id.bare)).setImageResource(R.drawable.a46);
				else if(bare >= 75) ((ImageView)findViewById(R.id.bare)).setImageResource(R.drawable.a45);
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("mia_un", PreferencesUtils.getString(this, "username"));
		params.put("pwd", PreferencesUtils.getString(this, "password"));
		params.put("babyid", mObj.getString("babyid"));

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/DownLoadDeviceInfo.ashx", JSON.toJSONString(params), loginHandler);
	}
}
